angular.module('UCX.Cmis')
.directive 'selectionArea',(ModelService,$window,$rootScope,$timeout)->
  restrict:'AECM'
  link:(scope,ele,attr)->
    ###
      ele[0] is parent node of all choseAble files or documents
      selectDiv is selection aera  选区
      body is body
      $('.nav-list').width() 选区基于 window对象  而 被选的文件是不是基于窗口的
      $('.fil-wrap').offset().top  被选文件不是基于窗口的  所以需要 加上  外部的  top值和left值
    ###
    body = angular.element($('body'))
    startTime=0
    startX=0
    startY=0
    selectDiv=null
    selList=null
    scope.ctrlClick=(obj)->
#      trigger ctrl click
      evt = document.createEvent('MouseEvents')
      evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, true, false, false, true, isMiddle ? 1 : 0, null )
      obj.dispatchEvent(evt)
    scope.handleMouseDown=(e)->
#      ### 注意这个selList 只有加载完成 才能正常使用 这个可能会是一个隐患
      selList=angular.element($('.select-list'))
      #       body click cancel pop-up
      $('body').click()
      $rootScope.multipleChooseFiles=[]
      $rootScope.multipleChooseFilesId=[]
      $rootScope.choseFile=$rootScope.currentFolder
      $timeout(()->
        $rootScope.$apply()
      ,0)
      e.preventDefault() if (e.preventDefault)
      if e.button==0
        startTime=new Date()
        selectDiv = document.createElement( "div" )
        selectDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px solid #aaa;background-color:#ccc;z-index:9999;filter:alpha(opacity:60);opacity:0.6;"
        selectDiv.id = "selectDiv"
        body.append(selectDiv)
        #        ie 的 e.clientY  e.y 两个值是不一样的  e.clientY和 其它浏览器是相同的
        startX = e.clientX || e.x
        startY = e.clientY || e.y
        #        console.log e
        #        console.log e.x || e.clientX
        selectDiv.style.left = startX + "px"
        selectDiv.style.top = startY + "px"
    scope.handleMouseMove=(e)->
      movedX = e.clientX || e.x
      movedY = e.clientY || e.y
      if startTime && Math.abs(movedX-startX)>2 && Math.abs(movedY-startY)>2
        e.preventDefault() if (e.preventDefault)
        selectDiv.style.left=Math.min(movedX,startX)+'px'
        selectDiv.style.top=Math.min(movedY,startY)+'px'
        selectDiv.style.height=Math.abs(movedY-startY)+'px'
        selectDiv.style.width=Math.abs(movedX-startX)+'px'
        l = selectDiv.offsetLeft
        t = selectDiv.offsetTop+$('.fil-wrap').scrollTop()
        w = selectDiv.offsetWidth
        h = selectDiv.offsetHeight
        for i in selList
          id=$(i).attr('data-id')
          waitL = i.offsetWidth + i.offsetLeft + $('.nav-list').width()
          waitT = i.offsetHeight + i.offsetTop + $('.fil-wrap').offset().top
          #          算法  选框左上角的点 必须在 碰撞对象右下角点 的左边上边  选款右下角的点 必须在 碰撞对象左上角点的 右边 下边
          if waitL > l && waitT > t && i.offsetLeft+ $('.nav-list').width() < l + w && i.offsetTop+ $('.fil-wrap').offset().top < t + h
            if $rootScope.multipleChooseFilesId.indexOf(id)== -1
              $rootScope.multipleChooseFiles.push(ModelService.getObjectById(id))
              $rootScope.multipleChooseFilesId.push(id)
              $timeout(()->
                $rootScope.$apply()
              ,0)
          else
            if $rootScope.multipleChooseFilesId.indexOf(id) != -1
              $rootScope.multipleChooseFiles.splice($rootScope.multipleChooseFiles.indexOf(ModelService.getObjectById(id)),1)
              $rootScope.multipleChooseFilesId.splice($rootScope.multipleChooseFilesId.indexOf(id),1)
              $timeout(()->
                $rootScope.$apply()
              ,0)

    #        console.log $rootScope.everyFolderCanNotDelete
    scope.handleMouseUp=(e)->

      if selectDiv
        $('#selectDiv').remove()
      if $rootScope.multipleChooseFiles.length==1
        $timeout(()->
          $rootScope.choseFile=$rootScope.multipleChooseFiles[0]
        )

      #      console.log($rootScope.multipleChooseFiles)
      #      for i in $rootScope.multipleChooseFiles
      #        $rootScope.multipleChooseFilesId.push(i.id)

      #      startTime 确保是点击拖动了
      if startTime
        e.stopPropagation()
        e.preventDefault()

        selectDiv = null
        startTime=null
    #        ie attachEvent
    #        chrome addEventListener
    if ele[0].addEventListener
#      console.log 'chrome'
      ele[0].addEventListener('mousedown',scope.handleMouseDown,false)
      $window.addEventListener('mousemove',scope.handleMouseMove,false)
      $window.addEventListener('mouseup',scope.handleMouseUp,false)
    else
      ele[0].attachEvent('mousedown',scope.handleMouseDown)
      $window.attachEvent('mousemove',scope.handleMouseMove)
      $window.attachEvent('mouseup',scope.handleMouseUp)
    return true